package com.whu.zj;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import java.util.HashMap;

import utils.NetworkUtil;

/**
 * 全局主线程控制器
 * Created by Administrator on 2016/2/21.
 */
public class UIHandler extends Handler {
    public static UIHandler uiHandler;

    private Context context;

    // 界面
    private HashMap<String, Activity> uis = new HashMap<String, Activity>();

    //　通过名字获取到界面
    public Activity getActivityByName(String name) {
        return uis.get(name);
    }

    public UIHandler() {
        super();
        context = MyApplication.share();
    }

    /**
     * 界面注册
     *
     * @param name
     * @param ui
     */
    public void register(String name, Activity ui) {
        uis.put(name, ui);
    }

    /**
     * 界面反注册
     *
     * @param name
     * @param ui
     */
    public void unregister(String name, Activity ui) {
        uis.remove(name);
    }

    @Override
    public void handleMessage(Message msg) {
        super.handleMessage(msg);

    }

    // 退出所有的界面
    public void finishiall() {
        for (Activity activity : uis.values()) {
            activity.finish();
        }
    }

    // 除了某节目外退出其他界面
    public void finishallbut(Activity target)
    {
        for(Activity activity:uis.values())
        {
            if (activity != target)
            {
                activity.finish();
            }
        }
    }

    public static interface UIAdapter {
        public String simpleName();
    }

    /**
     * 协助控制类
     */
    public static class BaseActivity  extends FragmentActivity implements UIAdapter {

        ProgressDialog pdg;

        public ProgressDialog getPdg() {
            return pdg;
        }

        public void showDialog() {
            pdg.show();
            pdg.setContentView(R.layout.window_progress);
            pdg.setCancelable(false);
        }

        public void closeDialog() {
            pdg.dismiss();
        }

        @Override
        public String simpleName() {
            return this.getClass().getSimpleName();
        }

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            pdg = new ProgressDialog(this);

            UIHandler.uiHandler.register(simpleName(), this);

            if (NetworkUtil.isNetworkAvailable(MyApplication.share()))
            {
                // 网络链接可用

            }
            else
            {
                // 网络链接不可用
                Toast.makeText(MyApplication.share(),"网络链接不可用,请打开！",Toast.LENGTH_SHORT).show();
                this.finish();
            }
        }

        @Override
        protected void onStart() {
            super.onStart();
            Button btn_back = (Button) findViewById(R.id.id_btn_back);
            if (btn_back == null) return;
            btn_back.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    finish();
                }
            });
        }

        @Override
        protected void onDestroy() {
            super.onDestroy();
            UIHandler.uiHandler.unregister(simpleName(), this);
        }
    }
}
